function[input, output, time_axis] = prepareanndata(k, b, time_array_min, x_TAL, y_TAL, z_TAL, ...
   x_GHC, y_GHC, z_GHC, x_PBC, y_PBC, z_PBC, x_CBB, y_CBB, z_CBB,  obj_TAL, obj_GHC, obj_PBC, obj_CBB )

% create training data

xTAL = x_TAL(b.beg(k):b.end(k));
yTAL = y_TAL(b.beg(k):b.end(k));
zTAL = z_TAL(b.beg(k):b.end(k));

xGHC = x_GHC(b.beg(k):b.end(k));
yGHC = y_GHC(b.beg(k):b.end(k));
zGHC = z_GHC(b.beg(k):b.end(k));

xPBC = x_PBC(b.beg(k):b.end(k));
yPBC = y_PBC(b.beg(k):b.end(k));
zPBC = z_PBC(b.beg(k):b.end(k));

xCBB = x_CBB(b.beg(k):b.end(k));
yCBB = y_CBB(b.beg(k):b.end(k));
zCBB = z_CBB(b.beg(k):b.end(k));

time_axis = time_array_min(b.beg(k):b.end(k));

%local time at observatories
tTAL = (time_axis + (obj_TAL.geospatial_lon/15)/24) - floor(time_axis + (obj_TAL.geospatial_lon/15)/24); %shift the axis to the local time
tCBB = (time_axis + (obj_CBB.geospatial_lon/15)/24) - floor(time_axis + (obj_CBB.geospatial_lon/15)/24); %shift the axis to the local time
tGHC = (time_axis + (obj_GHC.geospatial_lon/15)/24) - floor(time_axis + (obj_GHC.geospatial_lon/15)/24); %shift the axis to the local time
tPBC = (time_axis + (obj_PBC.geospatial_lon/15)/24) - floor(time_axis + (obj_PBC.geospatial_lon/15)/24); %shift the axis to the local time


% Now make cell structure for ANN input
% Let us assign TAL as local station and GHC as remote station Find local dF, dD

dTAL = 180/pi * atan( yTAL./ xTAL );
fTAL = sqrt(xTAL.^2 + yTAL.^2 + zTAL.^2);


fGHC = sqrt(xGHC.^2 + yGHC.^2 + zGHC.^2);
dGHC = 180/pi * atan( yGHC./ xGHC );

fPBC = sqrt(xPBC.^2 + yPBC.^2 + zPBC.^2);
dPBC = 180/pi * atan( yPBC./ xPBC );

fCBB = sqrt(xCBB.^2 + yCBB.^2 + zCBB.^2);
dCBB = 180/pi * atan( yCBB./ xCBB );


% make input and output targets. Trend is removed.
% 

% With time
% output = detrend(dGHC);
% input = detrend([dCBB fCBB zCBB tGHC' tCBB']);

% output = detrend(dGHC);
% input = detrend([dCBB fCBB zCBB fGHC tGHC' tCBB']);

% output = detrend(dPBC);
% input = detrend([dGHC fGHC zGHC fPBC tPBC' tGHC']);

% output = detrend(dGHC);
% input = detrend([dPBC fPBC zPBC tPBC' fGHC tGHC' ]);

output = detrend(dCBB);
input = detrend([dGHC fGHC zGHC tGHC' fCBB tCBB' ]);

return;
